package renguanyu.app.codebuilder.dao;

import javafx.collections.ObservableList;
import javafx.collections.ObservableMap;
import renguanyu.app.codebuilder.util.JDBCFactory;

public class TemplateDataDao {

    public ObservableList<ObservableMap<String, String>> query(String templateId) {
        StringBuilder sql = new StringBuilder();
        sql.append("SELECT * ");
        sql.append("FROM TEMP_DATA ");
        sql.append("WHERE TEMP_ID = '"+templateId+"' ");
        return JDBCFactory.get().query(sql.toString());
    }

    public boolean delete(String templateDataId) {
        StringBuilder sql = new StringBuilder("DELETE FROM TEMP_DATA WHERE TEMP_DATA_ID='"+templateDataId+"'");
        return JDBCFactory.get().dml(sql.toString());
    }

    public boolean save(String templateDataId, String templateId, String fileName, String fileData) {
        StringBuilder sql = new StringBuilder("insert into temp_data values('"+templateDataId+"','"+templateId+"','"+fileName+"','"+fileData+"')");
        return JDBCFactory.get().add(sql.toString());
    }


    public boolean update(String tempDataId, String fileName, String fileData) {
        StringBuilder sql = new StringBuilder();
        sql.append("UPDATE TEMP_DATA SET F_NAME='"+fileName+"',F_DATA='"+fileData+"' WHERE TEMP_DATA_ID = '"+tempDataId+"' ");
        return JDBCFactory.get().dml(sql.toString());
    }

    public boolean isExist(String tempDataId) {
        StringBuilder sql = new StringBuilder();
        sql.append("SELECT COUNT(*) AS TOTAL FROM TEMP_DATA WHERE TEMP_DATA_ID='"+tempDataId+"' ");
        ObservableMap<String, String> map = JDBCFactory.get().queryOne(sql.toString());
        Integer total = Integer.parseInt(map.get("TOTAL"));

        if(total>0){
            return true;
        }
        return false;
    }
}
